<%@ page contentType="text/html; charset=UTF-8" language="java" import="java.sql.*" errorPage="" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="Content-Script-Type" content="text/javascript" />
<meta http-equiv="Content-Script-Type" content="text/css" />
<title>修改营养素</title>
<link type="text/css" href="/css/tablecloth.css" rel="stylesheet" media="screen" />
<script type="text/javascript" src="/js/jquery-1.4.4.js"></script>
<script type="text/javascript" src="/js/tablecloth.js" ></script>
<script type="text/javascript" src="/js/tableinput.js"></script>
<script type="text/javascript">
function addNutriment()
{
	var form = document.getElementById("addNutrimentForm");
	form.name.value = 
		$("#nameInput").text();
	form.description.value = 
		$("#descriptionInput").text();
	form.unit.value = 
		$("#unitselector").val();
	form.submit();
	alert("添加成功");
}

function deleteNutriment(id)
{
	var form = document.getElementById("removeNutrimentForm");
	form.id.value = id;
	form.submit();
	alert("删除成功");
}
</script>
</head>

<body>
	<form action="/nutriment/addNutriment.do" method="post" id="addNutrimentForm">
		<input type="hidden" name="name" />
		<input type="hidden" name="description" />
		<input type="hidden" name="unit" />
	</form>
	
	<form action="/nutriment/removeNutriment.do" method="post" id="removeNutrimentForm">
		<input type="hidden" name="id" />
	</form>
	
	<%@ taglib uri="/WEB-INF/tld/list-taglib.tld" prefix="tj" %>
	<%@ page import="db.nutriment.Nutriment" %>
	<%@ page import="java.util.List" %>
	<table id="t" cellspacing="0" cellpadding="0">
		<tr>
			<th class="title1">营养素</th>
			<th class="title2">描述</th>
			<th class="title3">单位</th>
			<th class="title4">添加/删除</th>
		</tr>
		<tr>
			<% out.print("<td id='nameInput' needInput='true'>单击添加</td>"); %>
			<% out.print("<td id='descriptionInput' needInput='true'>单击添加</td>"); %>
			<td>
				<select id="unitselector">
					<option>ug</option>
					<option>mg</option>
					<option>g</option>
				</select>
			</td>
			<td>
				<input type='button' value='添加' onclick='addNutriment()'/>
			</td>
		</tr>
		<%
			@SuppressWarnings("unchecked")
			List<Nutriment> nutriment = (List<Nutriment>)request.getAttribute("nutriments");
			
			for(int i=0; i < nutriment.size();i++)
			{
			    out.print("<tr>");
				out.print("<td>" + nutriment.get(i).getName() + "</td>");
				out.print("<td>" + nutriment.get(i).getDescription() + "</td>");
				if(nutriment.get(i).getUnit() == null) {
					out.print("<td>无单位</td>");
				}
				else {
					out.print("<td>" + nutriment.get(i).getUnit() + "</td>");
				}
				out.print("<td><input type='button' value='删除' onclick=deleteNutriment(" + nutriment.get(i).getId() + ") /></td>");
				out.print("</tr>");
			}
		%>
	</table>
	<p>&nbsp;</p>
</body>
</html>
